{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copyright (C) 2016 - 2019 Pinard Liu(liujianping-ok@163.com)\n",
    "\n",
    "https://www.cnblogs.com/pinard\n",
    "\n",
    "Permission given to modify the code as long as you keep this declaration at the top\n",
    "\n",
    "tensorflow机器学习模型的跨平台上线 https://www.cnblogs.com/pinard/p/9251296.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "from sklearn.datasets.samples_generator import make_classification\n",
    "import tensorflow as tf\n",
    "X1, y1 = make_classification(n_samples=4000, n_features=6, n_redundant=0,\n",
    "                             n_clusters_per_class=1, n_classes=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4000, 6)\n",
      "(4000,)\n"
     ]
    }
   ],
   "source": [
    "print (X1.shape)\n",
    "print (y1.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "learning_rate = 0.01\n",
    "training_epochs = 600\n",
    "batch_size = 100\n",
    "\n",
    "x = tf.placeholder(tf.float32, [None, 6],name='input') # 6 features\n",
    "y = tf.placeholder(tf.float32, [None, 3]) # 3 classes\n",
    "\n",
    "W = tf.Variable(tf.zeros([6, 3]))\n",
    "b = tf.Variable(tf.zeros([3]))\n",
    "\n",
    "# softmax回归\n",
    "pred = tf.nn.softmax(tf.matmul(x, W) + b, name=\"softmax\") \n",
    "cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))\n",
    "optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)\n",
    "\n",
    "prediction_labels = tf.argmax(pred, axis=1, name=\"output\")\n",
    "\n",
    "init = tf.global_variables_initializer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4000, 3)\n"
     ]
    }
   ],
   "source": [
    "sess = tf.Session()\n",
    "sess.run(init)\n",
    "y2 = tf.one_hot(y1, 3)\n",
    "y2 = sess.run(y2)\n",
    "print (y2.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 0010 cost= 1.050110102\n",
      "Epoch: 0020 cost= 1.001712561\n",
      "Epoch: 0030 cost= 0.958428741\n",
      "Epoch: 0040 cost= 0.919634283\n",
      "Epoch: 0050 cost= 0.884774268\n",
      "Epoch: 0060 cost= 0.853360713\n",
      "Epoch: 0070 cost= 0.824967980\n",
      "Epoch: 0080 cost= 0.799225986\n",
      "Epoch: 0090 cost= 0.775815248\n",
      "Epoch: 0100 cost= 0.754458249\n",
      "Epoch: 0110 cost= 0.734916270\n",
      "Epoch: 0120 cost= 0.716981947\n",
      "Epoch: 0130 cost= 0.700476527\n",
      "Epoch: 0140 cost= 0.685244024\n",
      "Epoch: 0150 cost= 0.671149671\n",
      "Epoch: 0160 cost= 0.658075333\n",
      "Epoch: 0170 cost= 0.645918190\n",
      "Epoch: 0180 cost= 0.634587705\n",
      "Epoch: 0190 cost= 0.624004900\n",
      "Epoch: 0200 cost= 0.614099383\n",
      "Epoch: 0210 cost= 0.604809642\n",
      "Epoch: 0220 cost= 0.596081078\n",
      "Epoch: 0230 cost= 0.587864757\n",
      "Epoch: 0240 cost= 0.580117583\n",
      "Epoch: 0250 cost= 0.572800457\n",
      "Epoch: 0260 cost= 0.565879166\n",
      "Epoch: 0270 cost= 0.559322178\n",
      "Epoch: 0280 cost= 0.553101718\n",
      "Epoch: 0290 cost= 0.547192395\n",
      "Epoch: 0300 cost= 0.541571438\n",
      "Epoch: 0310 cost= 0.536218166\n",
      "Epoch: 0320 cost= 0.531113505\n",
      "Epoch: 0330 cost= 0.526240945\n",
      "Epoch: 0340 cost= 0.521584213\n",
      "Epoch: 0350 cost= 0.517129600\n",
      "Epoch: 0360 cost= 0.512863755\n",
      "Epoch: 0370 cost= 0.508774936\n",
      "Epoch: 0380 cost= 0.504852176\n",
      "Epoch: 0390 cost= 0.501085103\n",
      "Epoch: 0400 cost= 0.497464716\n",
      "Epoch: 0410 cost= 0.493982226\n",
      "Epoch: 0420 cost= 0.490630001\n",
      "Epoch: 0430 cost= 0.487400532\n",
      "Epoch: 0440 cost= 0.484286994\n",
      "Epoch: 0450 cost= 0.481283128\n",
      "Epoch: 0460 cost= 0.478383124\n",
      "Epoch: 0470 cost= 0.475581378\n",
      "Epoch: 0480 cost= 0.472873092\n",
      "Epoch: 0490 cost= 0.470253319\n",
      "Epoch: 0500 cost= 0.467717826\n",
      "Epoch: 0510 cost= 0.465262204\n",
      "Epoch: 0520 cost= 0.462882727\n",
      "Epoch: 0530 cost= 0.460575819\n",
      "Epoch: 0540 cost= 0.458337963\n",
      "Epoch: 0550 cost= 0.456166118\n",
      "Epoch: 0560 cost= 0.454057366\n",
      "Epoch: 0570 cost= 0.452008665\n",
      "Epoch: 0580 cost= 0.450017571\n",
      "Epoch: 0590 cost= 0.448081255\n",
      "Epoch: 0600 cost= 0.446197867\n",
      "优化完毕!\n",
      "0.8675\n"
     ]
    }
   ],
   "source": [
    "for epoch in range(training_epochs):\n",
    "\n",
    "    _, c = sess.run([optimizer, cost], feed_dict={x: X1, y: y2})\n",
    "    if (epoch+1) % 10 == 0:\n",
    "        print (\"Epoch:\", '%04d' % (epoch+1), \"cost=\", \"{:.9f}\".format(c))\n",
    "    \n",
    "print (\"优化完毕!\")\n",
    "correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y2, 1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "acc = sess.run(accuracy, feed_dict={x: X1, y: y2})\n",
    "print (acc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Froze 2 variables.\n",
      "Converted 2 variables to const ops.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'.\\\\rf.pb'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "graph = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, [\"output\"])\n",
    "tf.train.write_graph(graph, '.', 'rf.pb', as_text=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
